<%@page import="com.lmm.car.LoginAutoHome"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../common/taglib.jsp" %>
<%
	Map<String,String> param = LoginAutoHome.getInitParameter();
	String imageBase64  = LoginAutoHome.getDealerValidateCode();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<script type="text/javascript" src="${ctx}/js/jquery/1.11.1/jquery-1.11.1.js"></script>  
	<script type="text/javascript" src="${ctx}/js/rsa/RSA.js"></script>  
  	<script type="text/javascript" src="${ctx}/js/rsa/BigInt.js"></script>  
    <script type="text/javascript" src="${ctx}/js/rsa/Barrett.js"></script> 
    <script type="text/javascript">
    function enPasswordVal(){
    	var password = $("#password").val();
    	//密码加密
        setMaxDigits(129);
        var key = new RSAKeyPair($('#hidPublicKeyExponent').val(), "", $('#hidPublicKeyModulus').val());
        var pwdRtn = encryptedString(key, password);
        //var sourcePwd = decryptedString(key,pwdRtn);
        //window.alert(sourcePwd);
        $("#enPassword").val(pwdRtn);
    }
    
    function testLogin(){
    	 debugger;
    	 setMaxDigits(129);
         var key = new RSAKeyPair($('#hidPublicKeyExponent').val(), "", $('#hidPublicKeyModulus').val());
         var password= jQuery("#password").val();
         var pwdRtn = encryptedString(key, password);
         var userName = jQuery("#username").val();
         var requestVerificationToken = jQuery("#__RequestVerificationToken").val();
         var checkCodeDealer = jQuery("#checkCodeDealer").val();
         jQuery.ajax({
             type: "POST",
             url: 'test-login.jsp',
             data: { __RequestVerificationToken: requestVerificationToken, 
            	 UserNameDealer: userName, PasswordDealer: pwdRtn, 
            	 RedisKey: $("#rkey").val(), 
            	 checkCodeDealer: checkCodeDealer },
  
             success: function(msg) {
            	 debugger;
            	 msg = msg.trim();
                 if (msg == "0") {
                   window.alert('登录成功。');
                 } else {
                     var msgArray = msg.split("|");
                     //$("#password").val('');

                     if (parseInt(msgArray[1], 10) > 1) {
                         //$("#divDealerValidateCode").show();
                         //RefushImageDealer();
                    	 window.alert('验证码输入错误。');
                     }

                     if (msgArray[0] == 1) {
                    	 window.alert('登录次数过多,请一小时之后登录。');
                     } else if (msgArray[0] == 2) {
                    	 window.alert('用户不存在或者密码错误');
                     } else if (msgArray[0] == 3) {
                    	 window.alert('该用户帐户信息错误或访问受限');
                     } else if (msgArray[0] == 4) {
                    	 window.alert('请求错误，请刷新页面重新提交。');
                     } else if (msgArray[0] == 5) {
                    	 window.alert('验证码输入错误');
                     } else if (msgArray[0] == 6) {
                    	 window.alert('该账号没有关联店铺，不允许登录。');
                     } else if (msgArray[0] == 7) {
                    	  window.alert('请修改密码。');
                         //window.location.href = '/passport/Home/ChangePassword';
                     }
                 }
             },
             error: function(data) {
            	 window.alert('登录失败，请重试');
                 //$("#PasswordDealer").val('');
                 //$("#btnDealer").removeAttr("disabled");
             }
         })
    }
    function testAddNews(){
    	jQuery.ajax({
            type: "POST",
            url: 'test-add-news.jsp',
            success: function(msg) {
                  window.alert('添加成功。'+msg);
            },
            error: function(data) {
           	 window.alert('添加失败，请先登录。');
            }
    	});
    }
    </script> 
<title>加密登录测试</title>
</head>
<body>
	
		<form action="" method="post">
			<table>
				<tr>
					<td>token:</td>
					<td><input id="__RequestVerificationToken" name="__RequestVerificationToken" value="<%=param.get("__RequestVerificationToken") %>" /></td>
				</tr>
				<tr>
					<td>加密公匙：</td>
					<td><input id="rkey" name="rkey" value="<%=param.get("rkey") %>" /></td>
				</tr>
				<tr>
					<td>加密系数：</td>
					<td><input id="hidPublicKeyExponent" name="hidPublicKeyExponent" value="<%=param.get("hidPublicKeyExponent") %>" /></td>
				</tr>
				<tr>
					<td>加密公文：</td>
					<td><input id="hidPublicKeyModulus" name="hidPublicKeyModulus"  size="100" value="<%=param.get("hidPublicKeyModulus") %>" /></td>
				</tr>
				<tr>
					<td>用户名：</td>
					<td><input id="username" name="username" value="中拓瑞博" /></td>
				</tr>
				<tr>
					<td>密码：</td>
					<td><input id="password" name="password" value="ZTRB060708#"/></td>
				</tr>
				<tr>
					<td>加密密码：</td>
					<td><input  id="enPassword" name="enPassword" size="100" /></td>
				</tr>
				<tr>
					<td>验证码：</td>
					<td><input id="checkCodeDealer" name="checkCodeDealer" /><img src="<%=imageBase64 %>" /></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="button" value="加密" onclick="enPasswordVal();" />
						<input type="button" value="测试登录" onclick="testLogin();" />
						<input type="button" value="测试添加新闻" onclick="testAddNews();" />
					</td>
				</tr>
			</table>
		</form>
</body>
</html>